package com.jader.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jader.beans.Goods;
import com.jader.util.JaderApplication;
import com.jader.util.PreferenceUtil;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.zlp.jader.R;

public class CollectGoodsAdapter extends BaseAdapter {
	List<Goods> collect_goods = new ArrayList<Goods>();
	Context context;
	private LayoutInflater inflater;
	private HttpUtils httpUtils;
	private String url = JaderApplication.getIP() + "/Jader/client";

	public CollectGoodsAdapter(List<Goods> list, Context context) {
		super();
		this.collect_goods = list;
		this.context = context;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return collect_goods.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return collect_goods.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	ViewHolder viewHolder;

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.listview_collectgood, null);
			viewHolder = new ViewHolder();
			viewHolder.tv_collect_goodname = (TextView) convertView
					.findViewById(R.id.tv_collect_goodname);
			viewHolder.tv_collect_category = (TextView) convertView
					.findViewById(R.id.tv_collectgood_category);
			viewHolder.tv_collect_goodprice = (TextView) convertView
					.findViewById(R.id.tv_collect_goodprice);
			viewHolder.img_collect_delete = (ImageView) convertView
					.findViewById(R.id.img_collect_delete);
			viewHolder.img_collect_goodpic = (ImageView) convertView
					.findViewById(R.id.img_collectgood_goodpic);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		String goodname = collect_goods.get(position).getGoodsName();
		double goodprice = collect_goods.get(position).getPrice();
		String goodcategory = collect_goods.get(position).getCategory();
		String goodpicture = collect_goods.get(position).getPicture();
		Log.e("Pic", goodpicture);
		viewHolder.tv_collect_goodname.setText(goodname);
		viewHolder.tv_collect_category.setText("商品类别:" + goodcategory);
		viewHolder.tv_collect_goodprice.setText("￥ " + goodprice);
		JaderApplication.getBitmapUtils().display(
				viewHolder.img_collect_goodpic,
				JaderApplication.getIP()+"/Jader" + goodpicture);
		viewHolder.img_collect_delete
				.setOnClickListener(new Position(position));
		return convertView;
	}

	class ViewHolder {
		ImageView img_collect_goodpic, img_collect_delete;
		TextView tv_collect_goodname, tv_collect_goodprice,
				tv_collect_category;
	}

	class Position implements OnClickListener {
		int z;

		public Position(int i) {
			this.z = i;
		}

		@Override
		public void onClick(View v) {
			deleteCollectGood(z);
			;
		}

	}

	/** 取消商品收藏 */
	public void deleteCollectGood(final int local) {
		new com.jader.myviews.AlertDialog(context).builder()
				.setTitle("是否取消收藏？")
				.setPositiveButton("确定", new OnClickListener() {

					@Override
					public void onClick(View v) {
						int goodid = collect_goods.get(local).getGoodsid();
						Log.e("position", "" + goodid);
						httpUtils = new HttpUtils();
						RequestParams params = new RequestParams();
						params.addBodyParameter("type", ""
								+ JaderApplication.COLLECT_GOODS_DELETE);
						params.addBodyParameter("userid", PreferenceUtil
								.getInstance(context).getShareUserId());
						params.addBodyParameter("goodsid", "" + goodid);
						httpUtils.send(HttpMethod.POST, url, params,
								new RequestCallBack<String>() {

									@Override
									public void onFailure(HttpException arg0,
											String arg1) {
										JaderApplication.show(context, "取消失败");

									}

									@Override
									public void onSuccess(
											ResponseInfo<String> arg0) {
										JaderApplication
												.show(context, "已经取消收藏");
										collect_goods.remove(local);
										notifyDataSetChanged();
									}
								});
					}
				}).setNegativeButton("取消", null).show();
	}
}
